package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CusDevices;
import com.ruoyi.system.domain.CusPositions;
import com.ruoyi.system.service.ICusDevicesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2022-01-12
 */
@Controller
@RequestMapping("/system/devices")
public class CusDevicesController extends BaseController
{
    private String prefix = "system/devices";

    @Autowired
    private ICusDevicesService cusDevicesService;

    @RequiresPermissions("system:devices:view")
    @GetMapping()
    public String devices()
    {
        return prefix + "/devices";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("system:devices:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CusDevices cusDevices)
    {
    	if(cusDevices.getDevicePositions() != null && !cusDevices.getDevicePositions().isEmpty()) {
    		String sqlString = "";
    		String[] itemStrings = cusDevices.getDevicePositions().split(",");
    		for(int i = 0; i < itemStrings.length; i++) {
    			String iString = itemStrings[i];
    			
    			sqlString += iString; 
    			
    			if(i == (itemStrings.length - 1)) {
    				sqlString += "";
    			}else {
    				sqlString += ",";
    			}
    		}
    		cusDevices.setDevicePositionsarr(itemStrings);
    	}
    	
        startPage();
        List<CusDevices> list = cusDevicesService.selectCusDevicesList(cusDevices);
        return getDataTable(list);
    }
    
    /**
     * 查询列表（select2）
     */
    //@RequiresPermissions("system:devices:list")
    @RequestMapping("/select2")
    @ResponseBody
    public Object select2(CusDevices cusDevices, String q)
    {
    	cusDevices.setDeviceName(q);
    	
    	List<CusDevices> list = cusDevicesService.selectCusDevicesList(cusDevices);
		return getSelect2(list, new Function<CusDevices, HashMap<String,String>>() {
			@Override
			public HashMap<String, String> apply(CusDevices t) {
				HashMap<String, String> data = new HashMap<>();
				data.put("id", t.getID() + "");
				data.put("text", t.getDeviceName());
				data.put("leaf", "true");
				return data;
			}
		});
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("system:devices:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CusDevices cusDevices)
    {
        List<CusDevices> list = cusDevicesService.selectCusDevicesList(cusDevices);
        ExcelUtil<CusDevices> util = new ExcelUtil<CusDevices>(CusDevices.class);
        return util.exportExcel(list, "设备管理数据");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("system:devices:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CusDevices cusDevices)
    {
        return toAjax(cusDevicesService.insertCusDevices(cusDevices));
    }

    /**
     * 修改设备管理
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        CusDevices cusDevices = cusDevicesService.selectCusDevicesByID(ID);
        mmap.put("cusDevices", cusDevices);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("system:devices:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CusDevices cusDevices)
    {
        return toAjax(cusDevicesService.updateCusDevices(cusDevices));
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("system:devices:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cusDevicesService.deleteCusDevicesByIDs(ids));
    }
}
